﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using System.Data;
using AspNetCs.DALFactory;
using System.Collections;
using AspNetCs.Model;
using System.Web;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;
namespace AspNetCs.Bll
{
    public class Memorial
    {
        private IMemorial MemorialDAl = Conn.CreateMemorial();
        public string RndNum(ArrayList list, int RndLen)
        {
            return MemorialDAl.RndNum(list, RndLen);
        }

        /// <summary>
        /// 根据条件返回DataTable。filter可以为" "； filter 请从" Where " 关键字开始
        /// </summary>
        /// <param name="filter">filter可以为" ", filter= " Where 字段名=值 [and 字段名2=值] "</param>
        /// <returns></returns>
        public DataTable GetDataTable(string filter)
        {
            return MemorialDAl.GetDataTable(filter);
        }

        /// <summary>
        /// 检查馆号是否存在
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public bool NumberIsBeing(string num)
        {
            bool Result = false;
            DataTable dt = GetDataTable(" where Numid='" + num + "'");
            if (dt.Rows.Count > 0)
            {
                Result = true;
            }

            return Result;
        }



        public int Add(memorial_model md, out string Msg)
        {
            int Result = 0;
            Msg = "";
            if (md == null) { Msg = "参数错误！"; return -1; }
            Result = MemorialDAl.Add(md);
            if (Result <= 0) { Msg = "添加失败!"; }
            else
            {
                Msg = "添加成功！";
            }
            return Result;
        }

        /// <summary>
        /// 在线追思更新
        /// </summary>
        public int IndexOnTop(memorial_model md)
        {
            if (md == null) return -1;
            return MemorialDAl.IndexOnTop(md);

        }

        public memorial_model GetModel(int id)
        {
            return GetModel(" where id=" + id);
        }

        public memorial_model GetModel(string filter)
        {
            memorial_model md = new memorial_model();
            foreach (DataRow dr in GetDataTable(filter).Rows)
            {
                md = MemorialDAl.GetModel(dr);
            }
            return md;
        }



        public memorial_model GetModel(DataRow dr)
        {
            memorial_model md = new memorial_model();
            if (dr != null)
            {
                md = MemorialDAl.GetModel(dr);
            }

            return md;
        }






        public int MemorialPwd_Set(memorial_model md)
        {
            if (md == null) return -1;
            return MemorialDAl.MemorialPwd_Set(md);
        }

        public int Update(memorial_model md, out string Msg)
        {
            int Result = 0;
            Msg = "";
            if (md == null) { Msg = "参数错误！"; return -1; }
            Result = MemorialDAl.Update(md);
            if (Result <= 0) { Msg = "修改失败!"; }
            else
            {
                Msg = "修改成功！";
            }
            return Result;
        }
        /// <summary>
        /// 标识用户删除信息
        /// </summary>
        /// <param name="filter">条件,不跟“Where”关键字</param>
        /// <returns></returns>
        public int UserDel(string filter)
        {
            if (filter == "") return -1;
            return MemorialDAl.UserDel(filter);

        }
        /// <summary>
        /// 根据条件彻底删除
        /// </summary>
        /// <param name="filter">Where条件,不跟“Where”关键字</param>
        /// <returns></returns>
        public int Del(string filter)
        {
            if (filter == "") return -1;
            return MemorialDAl.Del(filter);
        }

        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 0表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            return MemorialDAl.SetIntro(Id, CancleOrIntro);
        }


        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            return MemorialDAl.SetCheck(Id, CancleOrIntro);
        }

        /// <summary>
        /// 设置在线追思或者取消
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置在线追思 0表示取消</param>
        /// <returns></returns>
        public int SetOnlineSign(int Id, int CancleOrIntro)
        {
            return MemorialDAl.SetOnlineSign(Id, CancleOrIntro);
        }

        /// <summary>
        /// 根据当前馆号,生成用模板
        /// </summary>
        /// <param name="NumStr">馆号</param>
        /// <param name="chel"></param>
        /// <param name="systemConfig">系统配制信息</param>
        /// <param name="htmlpath">生成的路径</param>
        /// <returns></returns>
        public bool CurrTempleContent(string NumStr, Channel_Type chel, SiteConfigInfo systemConfig, string htmlpath)
        {
            bool Result = false;
            StringBuilder stb = new StringBuilder();
            memorial_model md = new memorial_model();
            try
            {

                DataTable dt = GetDataTable(" Where  Numid='" + NumStr + "'");
                dt.TableName = "memorial";
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        md = GetModel(dr);
                        break;
                    }
                    if (md.TempleId > 0)
                    {
                        Temple_Model TmpMd = TempleBll.CreateNew.GetModel(md.TempleId);
                        if (TmpMd.Id > 0)
                        {
                            string TempleUrl = systemConfig.InstallDir + StringHelper.ReadConfig("TempleSkin") + chel.ToString() + "/" + TmpMd.TemplePath + "/index.html";
                            string Templepath = HttpContext.Current.Server.MapPath(TempleUrl);//模板的真实路径

                            StringBuilder newstr = new StringBuilder();

                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/CheckIsPwd.Aspx?id=" + md.Id + "&rnd=" + new Random().Next(1000, 9999999) + "'></script>");
                            newstr.AppendLine("<script>");
                            newstr.AppendLine("<!--");
                            newstr.AppendLine("var Numid='" + md.NumId + "';");//纪念馆馆号
                            newstr.AppendLine("var id='" + md.Id + "';");//纪念馆ID
                            newstr.AppendLine("var UserID='" + md.CreateId + "';");//纪念馆名称
                            newstr.AppendLine("var Pid='" + md.Province + "';");//省份
                            newstr.AppendLine("var CityID='" + md.City + "';");//城市

                            newstr.AppendLine("-->");
                            newstr.AppendLine("</script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/jquery.js'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/public.js'></script>");



                            stb = TempleBll.CreateNew.WriteHtml(Templepath, newstr.ToString());//读取模板内容
                            stb = Script_Lable.ScriptLable_Replace(stb, chel, md.Id.ToString());
                            GetStringData_lable lbReplace = new GetStringData_lable();
                            string ContentValue = lbReplace.GetResultStr(stb, dt);
                            Result = TempleBll.CreateNew.CreateHtmlFile(ContentValue, htmlpath);

                        }
                    }
                }
            }
            catch
            {
                Result = false;
            }

            return Result;
        }


        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            return MemorialDAl.SetMusic(Id, MusicId);
        }

        public void filterUpdate(string Upfilter, string filter)
        {
            MemorialDAl.filterUpdate(Upfilter, filter);
        }


    }

    public sealed class Memorial_Bll
    {


        /// <summary>
        /// 实例化该类
        /// </summary>
        public static Memorial CreateNew
        {
            get
            {
                return new Memorial();
            }
        }



    }



}
